package com.pzr.soccer.config.redis;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {


/*
    @SuppressWarnings("rawtypes")
    @Bean
    public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
        //默认的序列化策略是JDK的，所以在存储的时候键和值都会先被序列化后再存储
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        //1.修改键的序列化策略
        template.setKeySerializer(new StringRedisSerializer());
        //2.修改hash类型的value中key的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        //4.设置value的序列化策略
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //5.修改hash类型value中value的序列化方式
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.registerModule(new JavaTimeModule());
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        template.afterPropertiesSet();
        return template;

    }*/

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        // 解决jackson2无法反序列化LocalDateTime的问题
        ObjectMapper om = new ObjectMapper();
        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        om.registerModule(new JavaTimeModule());

        // redis key的序列化
        StringRedisSerializer keyRedisSerializer = new StringRedisSerializer();
        // redis value的序列化
        GenericJackson2JsonRedisSerializer valueRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setKeySerializer(keyRedisSerializer);
        redisTemplate.setValueSerializer(valueRedisSerializer);
        redisTemplate.setHashKeySerializer(keyRedisSerializer);
        redisTemplate.setHashValueSerializer(valueRedisSerializer);

        return redisTemplate ;
    }


/*     @Bean("UserRedisTemplate")
   public RedisTemplate<String, GameWrapper> redisTemplate(RedisConnectionFactory factory) {
             RedisTemplate<String, GameWrapper> template = new RedisTemplate<>();
              template.setConnectionFactory(factory);
                Jackson2JsonRedisSerializer<GameWrapper> j2jrs = new Jackson2JsonRedisSerializer<>(GameWrapper.class);
             ObjectMapper om = new ObjectMapper();
               om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
               // 解决jackson2无法反序列化LocalDateTime的问题
               om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
               om.registerModule(new JavaTimeModule());
               om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
              j2jrs.setObjectMapper(om);
                // 序列化 value 时使用此序列化方法
              template.setValueSerializer(j2jrs);
              template.setHashValueSerializer(j2jrs);
              // 序列化 key 时
               StringRedisSerializer srs = new StringRedisSerializer();
              template.setKeySerializer(srs);
               template.setHashKeySerializer(srs);
             template.afterPropertiesSet();
              return template;
           }*/
}
